package com.sgb.kjwl.view.ui.fragment.machine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.amap.api.maps.model.LatLng;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCarsInfoBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.transport.CarHallEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity4;
import com.sgb.kjwl.view.ui.activity.home.TransportSearchActivity;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.VehicleDetailActivity;
import com.sgb.kjwl.view.ui.adapter.home.VehicleHallAdapter;
import com.sgb.kjwl.viewmodel.transport.CarsInfoModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Description: 车辆列表改版3
 * Author zhengkewen
 */
public class CarInfoFragment extends AppLazyFragment {
    private FragmentCarsInfoBinding binding;

    private VehicleHallAdapter vehicleOwnerAdapter;
    private List<GoodsInfoTypeEntity3> carsTypeData;
    private List<GoodsInfoTypeEntity3> carsLengthData;

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private int page = 1;

    private String searchText;
    @Inject
    CarsInfoModel carsInfoModel;

    public LatLng currentLatLng = new LatLng(NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "")),
            NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "")));


    //列表
    public void getCarsInfoData(List<CarHallEntity> list, int totalRow, boolean isLoadMore) {
        binding.carsList.refreshComplete();
        if (isLoadMore) {
            binding.carsList.loadMoreComplete();
            if (list.size() > 0) {
                vehicleOwnerAdapter.addDatas(list);
                binding.carsList.setNoMore(false);
            } else {
                binding.carsList.setNoMore(true);
            }
            binding.carsList.setVisibility(View.VISIBLE);
        } else {
            binding.carsList.refreshComplete();
            if (list.size() > 0) {
                binding.carsList.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                vehicleOwnerAdapter.setDatas(list);
                binding.carsList.setLoadingMoreEnabled(true);
            } else {
                vehicleOwnerAdapter.setDatas(null);
                binding.carsList.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
        EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_CAR_COUNT).setCount(totalRow));
    }

    //获取车型
    public void getCarsTypeData(List<GoodsInfoTypeEntity3> list) {
        if (null != list && list.size() > 0) {
            carsTypeData = list;
        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }

    //获取车长
    public void getCarsLengthData(List<GoodsInfoTypeEntity3> list) {
        if (null != list && list.size() > 0) {
            carsLengthData = list;
        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder()
                .appComponent(appComponent)
                .machineFragModule(new MachineFragModule(this))
                .build()
                .inject(this);
    }

    public static CarInfoFragment newInstance() {
        CarInfoFragment fragment = new CarInfoFragment();
        return fragment;
    }

    public static CarInfoFragment newInstance(String keyWord) {
        CarInfoFragment fragment = new CarInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("KEY_WORD", keyWord);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_cars_info, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (getArguments() != null) {
            searchText = getArguments().getString("KEY_WORD");
            carsInfoModel.searchCars(SearchActivity4.SEARCHTYPE_LOGISTICS_CARS, searchText, false);
        }
        initView();
    }

    public void initView() {
        binding.setViewModel(carsInfoModel);
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.carsList.setVisibility(View.GONE);
        binding.carsList.setFootViewText("加载中", "我也是有底线的");
        if (vehicleOwnerAdapter == null) {
            vehicleOwnerAdapter = new VehicleHallAdapter(getActivity(), null);
        }
        binding.carsList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        binding.carsList.setAdapter(vehicleOwnerAdapter);
        vehicleOwnerAdapter.setOnItemClick(new AdapterOnItemClick<CarHallEntity>() {
            @Override
            public void onItemClick(CarHallEntity carsInfoEntity3, int position) {
                //跳转车辆详情
                VehicleDetailActivity.start(getActivity(), carsInfoEntity3.getDriverAuthNo());
            }
        });
        binding.carsList.setLoadingMoreEnabled(true);
        binding.carsList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                carsInfoModel.searchCars(SearchActivity4.SEARCHTYPE_LOGISTICS_CARS, searchText, false);
            }

            @Override
            public void onLoadMore() {
                carsInfoModel.searchCars(SearchActivity4.SEARCHTYPE_LOGISTICS_CARS, searchText, true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {

    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.SEARCH_CARS) {
            searchText = (String) event.getMessageData();
            if (this.getActivity() instanceof SearchActivity4 || this.getActivity() instanceof TransportSearchActivity) {
//                if (tabSwitch && vehicleOwnerAdapter != null && vehicleOwnerAdapter.getDatas() != null && vehicleOwnerAdapter.getDatas().size() > 0) {
//                } else {
                if (vehicleOwnerAdapter != null) {
                    vehicleOwnerAdapter.setDatas(null);
                }
                carsInfoModel.searchCars(SearchActivity4.SEARCHTYPE_LOGISTICS_CARS, searchText, false);
//                }
            }
            EventBus.getDefault().removeStickyEvent(event);
        }
    }

    public void refreshChildView(String mKeyWord) {
        carsInfoModel.searchCars(SearchActivity4.SEARCHTYPE_LOGISTICS_CARS, mKeyWord, false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
